<!DOCTYPE HTML>
<html lang="zh-CN">


<head><meta name="generator" content="Hexo 3.8.0">
    <!-- hexo-inject:begin --><!-- hexo-inject:end --><meta charset="utf-8">
    <meta name="keywords" content="shell相关操作, yyancy">
    <meta name="description" content="echo# 默认会换行
echo &#34;welcome to bash&#34; 
echo &#39;welcome to bash&#39;

# 双引号可以解释变量,特殊字符.单引号只当做字符串
echo &#34;$SHELL&#34; # /bin/bash
echo &#39;$">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>shell相关操作 | yyancy</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">
    
    <script src="/libs/jquery/jquery.min.js"></script>
    
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css">
<link rel="stylesheet" href="/css/prism-line-numbers.css" type="text/css"><!-- hexo-inject:begin --><!-- hexo-inject:end --></head>


<body>
    <!-- hexo-inject:begin --><!-- hexo-inject:end --><header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper head-container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">yyancy</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/contact" class="waves-effect waves-light">
      
      <i class="fas fa-comments" style="zoom: 0.6;"></i>
      
      <span>留言板</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友情链接</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>

<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">yyancy</div>
        <div class="logo-desc">
            
            yyancy的博客
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/contact" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-comments"></i>
			
			Contact
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友情链接
		</a>
          
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://github.com/blinkfox/hexo-theme-matery" class="waves-effect waves-light" target="_blank">
                <i class="fab fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>
</div>

        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://github.com/blinkfox/hexo-theme-matery" class="github-corner tooltipped hide-on-med-and-down" target="_blank" data-tooltip="Fork Me" data-position="left" data-delay="50">
    <svg viewbox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"/>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"/>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"/>
    </svg>
</a>
        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/featureimages/6.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">shell相关操作</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        margin: 35px 0 15px 0;
        padding-left: 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        height: calc(100vh - 250px);
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #toc-content .is-active-link::before {
        background-color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/Linux/">
                                <span class="chip bg-color">Linux</span>
                            </a>
                        
                            <a href="/tags/shell/">
                                <span class="chip bg-color">shell</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2020-04-01
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>更新日期:&nbsp;&nbsp;
                    2020-04-11
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    4k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp;
                    16 分
                </div>
                
				
                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
            
        </div>
        <hr class="clearfix">
        <div class="card-content article-card-content">
            <div id="articleContent">
                <h2 id="echo"><a href="#echo" class="headerlink" title="echo"></a>echo</h2><pre class="line-numbers language-bash"><code class="language-bash"><span class="token comment" spellcheck="true"># 默认会换行</span>
<span class="token keyword">echo</span> <span class="token string">"welcome to bash"</span> 
<span class="token keyword">echo</span> <span class="token string">'welcome to bash'</span>

<span class="token comment" spellcheck="true"># 双引号可以解释变量,特殊字符.单引号只当做字符串</span>
<span class="token keyword">echo</span> <span class="token string">"<span class="token variable">$SHELL</span>"</span> <span class="token comment" spellcheck="true"># /bin/bash</span>
<span class="token keyword">echo</span> <span class="token string">'<span class="token variable">$SHELL</span>'</span> <span class="token comment" spellcheck="true"># $SHELL </span>

<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h3 id="printf"><a href="#printf" class="headerlink" title="printf"></a>printf</h3><p>和echo一样可以输出字符,但这个可以进行更多格式化</p>
<pre class="line-numbers language-bash"><code class="language-bash">$ <span class="token function">printf</span> <span class="token string">"Hello world"</span>
<span class="token comment" spellcheck="true"># No Name Mark</span>
<span class="token function">printf</span> <span class="token string">"%-5s %-10s %-4s\n"</span> No Name Mark
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<h3 id="使用变量"><a href="#使用变量" class="headerlink" title="使用变量"></a>使用变量</h3><pre class="line-numbers language-bash"><code class="language-bash">var<span class="token operator">=</span><span class="token string">"value"</span> <span class="token comment" spellcheck="true">#引号可以没有</span>
<span class="token keyword">echo</span> <span class="token variable">$var</span> <span class="token comment" spellcheck="true"># value</span>
<span class="token keyword">echo</span> <span class="token variable">${var}</span> <span class="token comment" spellcheck="true"># value 这样也可以输出</span>
<span class="token comment" spellcheck="true"># 当var有值,且不为空时,则使用var2</span>
<span class="token keyword">echo</span> <span class="token variable">${var:+ var2}</span>

length<span class="token operator">=</span><span class="token variable">${#var}</span> <span class="token comment" spellcheck="true"># 获取字符长度</span>
<span class="token comment" spellcheck="true"># 获取当前使用的shell</span>
<span class="token keyword">echo</span> <span class="token variable">$SHELL</span>
<span class="token keyword">echo</span> <span class="token variable">$0</span>
<span class="token comment" spellcheck="true"># UID环境变量保存的当前用户ID,root用户是0</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="测试当前是否是root用户"><a href="#测试当前是否是root用户" class="headerlink" title="测试当前是否是root用户"></a>测试当前是否是root用户</h4><pre class="line-numbers language-bash"><code class="language-bash"><span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable">$UID</span> -ne 0 <span class="token punctuation">]</span> <span class="token punctuation">;</span> <span class="token keyword">then</span> <span class="token comment" spellcheck="true"># [ 前后必须有空格</span>
    <span class="token keyword">echo</span> <span class="token string">'None root user. Please run as root.
else
    echo '</span>Root user'
<span class="token keyword">fi</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="修改Bash提示字符"><a href="#修改Bash提示字符" class="headerlink" title="修改Bash提示字符"></a>修改Bash提示字符</h4><pre class="line-numbers language-bash"><code class="language-bash"><span class="token comment" spellcheck="true"># bash的提示字符显示的是PS1环境变量,默认在~/.bashrc中设置</span>
<span class="token function">cat</span> ~/.bashrc <span class="token operator">|</span> <span class="token function">grep</span> PS1
PS1<span class="token operator">=</span><span class="token string">"prompt>"</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<h3 id="执行算数运算"><a href="#执行算数运算" class="headerlink" title="执行算数运算"></a>执行算数运算</h3><pre class="line-numbers language-bash"><code class="language-bash"><span class="token comment" spellcheck="true"># 可以用let,(()),[]</span>
no1<span class="token operator">=</span>4<span class="token punctuation">;</span>
no2<span class="token operator">=</span>5<span class="token punctuation">;</span>
<span class="token comment" spellcheck="true"># let 命令可以自增/减</span>
<span class="token keyword">let</span> no1++
<span class="token keyword">let</span> no-<span class="token operator">=</span>6 <span class="token comment" spellcheck="true"># 等于 no=no-6</span>

<span class="token comment" spellcheck="true"># [] 和let差不多</span>
result<span class="token operator">=</span>$<span class="token punctuation">[</span> no1 + no2 <span class="token punctuation">]</span>
<span class="token comment" spellcheck="true"># 也可以带$前缀</span>
result<span class="token operator">=</span>$<span class="token punctuation">[</span> <span class="token variable">$no1</span> + 5 <span class="token punctuation">]</span>

<span class="token comment" spellcheck="true"># (()) 操作符中的变量必须加$.</span>
result<span class="token operator">=</span><span class="token variable"><span class="token variable">$((</span> no1 <span class="token operator">+</span> <span class="token number">50</span> <span class="token variable">))</span></span>

<span class="token comment" spellcheck="true"># expr 也可以计算</span>
result<span class="token operator">=</span><span class="token variable"><span class="token variable">`</span><span class="token function">expr</span> 3 + 4<span class="token variable">`</span></span>
result<span class="token operator">=</span><span class="token variable"><span class="token variable">$(</span><span class="token function">expr</span> $no1 + 5<span class="token variable">)</span></span>


<span class="token comment" spellcheck="true"># 上面的不支持浮点数运算 bc可以</span>
<span class="token keyword">echo</span> <span class="token string">"4 * 0.56"</span> <span class="token operator">|</span> <span class="token function">bc</span>
2.24
no<span class="token operator">=</span>54<span class="token punctuation">;</span>
result<span class="token operator">=</span>`echo <span class="token string">"<span class="token variable">$no</span> * 1.5"</span> <span class="token operator">|</span> bc`
<span class="token keyword">echo</span> <span class="token variable">$result</span>
81.0
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="文件重定向"><a href="#文件重定向" class="headerlink" title="文件重定向"></a>文件重定向</h2><p>在交互式shell中,有3个特殊的文件描述符</p>
<ul>
<li>0 标准输入</li>
<li>1 标准输出</li>
<li>2 标准错误</li>
</ul>
<p>使用<code>fd&gt;</code>可以将fd指向的文件描述符重定向到相关位置.特殊的重定向符号<code>&amp;&gt;</code>可以将STDERR和STDOUT的输出重定向到同一个输出文件 .</p>
<p>使用<code>&gt;&amp;fd</code>命令可以将相关信息送入到fd相关的文件中去.</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token comment" spellcheck="true"># 将信息输出到标准错误</span>
<span class="token keyword">echo</span> <span class="token string">"This is an error"</span> <span class="token operator">></span><span class="token operator">&amp;</span>2
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p>当命令错误退出时,会返回一个非0退出状态.退出状态<strong>可以用$?获得</strong></p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token keyword">echo</span> <span class="token string">"hello"</span> <span class="token operator">></span> temp.txt <span class="token comment" spellcheck="true"># 若文件存在,清空内容</span>
<span class="token keyword">echo</span> <span class="token string">"hello"</span> <span class="token operator">>></span> temp.txt <span class="token comment" spellcheck="true"># 追加</span>
<span class="token comment" spellcheck="true"># 表示将标准错误的内容重定向到err.txt文件中</span>
<span class="token function">ls</span> <span class="token keyword">.</span> 2<span class="token operator">></span> err.txt <span class="token comment" spellcheck="true"># 错误重定向</span>
<span class="token comment" spellcheck="true"># 表示将标准错误的内容重定向到err.txt文件中</span>
<span class="token function">ls</span> <span class="token keyword">.</span> 2<span class="token operator">></span>err.txt 1<span class="token operator">></span>out.txt <span class="token comment" spellcheck="true"># 内容重定向到out.txt,错误到err.txt</span>

<span class="token comment" spellcheck="true"># 将标准输入重定向到o.txt,将标准错误重定向到标准输入</span>
<span class="token comment" spellcheck="true"># 顺序不能颠倒,若2>&amp;1 >o.txt,则标准错误会指向显示器,1之后的改变不影响标准错误</span>
cmd <span class="token operator">></span> o.txt 2<span class="token operator">></span><span class="token operator">&amp;</span>1
<span class="token comment" spellcheck="true"># 将stderr装换成stdout,使得都被重定向到一个文件中</span>
cmd <span class="token operator">&amp;</span><span class="token operator">></span> output.txt

<span class="token comment" spellcheck="true"># 使用重定向符号&lt; 用文件来读取标准输入 </span>
<span class="token function">cat</span> <span class="token operator">&lt;</span> t.txt
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h5 id="创建输出文件描述符"><a href="#创建输出文件描述符" class="headerlink" title="创建输出文件描述符"></a>创建输出文件描述符</h5><p>若想在脚本运行期间都重定向相关fd.可以使用<code>exec 1&gt;dest</code>,这个会将标准输入在脚本执行期间重定向到dest.重定向输入可以<code>exec 0&lt; src</code>,这回在脚本执行期间,都在src中读取而不是标准输入.</p>
<p>也可以创建新的文件描述符<code>exec fd&gt;dest</code>,或<code>exec fd&gt;&gt;dest</code>.后一个是追加</p>
<h4 id="打开文件"><a href="#打开文件" class="headerlink" title="打开文件"></a>打开文件</h4><pre class="line-numbers language-bash"><code class="language-bash"><span class="token comment" spellcheck="true"># 可以用exec命令打开文件</span>
<span class="token function">exec</span> 3<span class="token operator">&lt;</span>input.txt <span class="token comment" spellcheck="true"># 使用文件描述符3打开并读取文件</span>
<span class="token comment" spellcheck="true"># 可以这样使用</span>
<span class="token keyword">echo</span> this is a <span class="token function">test</span> line <span class="token operator">></span> input.txt
<span class="token function">exec</span> 3<span class="token operator">&lt;</span>input.txt

cat<span class="token operator">&lt;</span><span class="token operator">&amp;</span>3
this is a <span class="token function">test</span> line

<span class="token comment" spellcheck="true"># 追加模式写入</span>
<span class="token function">exec</span> 5<span class="token operator">>></span>input.txt
<span class="token keyword">echo</span> appended line <span class="token operator">></span><span class="token operator">&amp;</span>5
<span class="token function">cat</span> input.txt
newline
appended line
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h5 id="恢复重定向的文件描述符"><a href="#恢复重定向的文件描述符" class="headerlink" title="恢复重定向的文件描述符"></a>恢复重定向的文件描述符</h5><p>在脚本执行期间,将永久重定向的文件描述符恢复为原来的地点.</p>
<p>下面的例子中, 首先，脚本将文件描述符3重定向到文件描述符1的当前位置，也就是STDOUT。这意味着任何发送给文件描述符3的输出都将出现在显示器上。</p>
<p>第二个exec命令将STDOUT重定向到文件， shell现在会将发送给STDOUT的输出直接重定向到<br>输出文件中。 但是，文件描述符3仍然指向STDOUT原来的位置，也就是显示器。如果此时将输出<br>数据发送给文件描述符3，它仍然会出现在显示器上，尽管STDOUT已经被重定向了 </p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">exec</span> 3<span class="token operator">></span><span class="token operator">&amp;</span>1
<span class="token function">exec</span> 1<span class="token operator">></span>dest
<span class="token punctuation">..</span>. <span class="token comment" spellcheck="true"># do something</span>
<span class="token function">exec</span> 1<span class="token operator">></span><span class="token operator">&amp;</span>3
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>
<h5 id="创建输入文件描述符"><a href="#创建输入文件描述符" class="headerlink" title="创建输入文件描述符"></a>创建输入文件描述符</h5><p>文件描述符6用来保存STDIN的位置。然后脚本将STDIN重定向到一个文件。<br>read命令的所有输入都来自重定向后的STDIN（也就是输入文件）。<br>在读取了所有行之后，脚本会将STDIN重定向到文件描述符6，从而将STDIN恢复到原先的<br>位置。 </p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">exec</span> 6<span class="token operator">&lt;</span><span class="token operator">&amp;</span>0
<span class="token function">exec</span> 0<span class="token operator">&lt;</span>src
<span class="token punctuation">..</span> <span class="token comment" spellcheck="true">#do something</span>
<span class="token function">exec</span> 0<span class="token operator">&lt;</span><span class="token operator">&amp;</span>6
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>
<h5 id="创建读写文件描述符"><a href="#创建读写文件描述符" class="headerlink" title="创建读写文件描述符"></a>创建读写文件描述符</h5><p>使用<code>exec fd&lt;&gt; dest</code> 可以创建读写的文件描述符</p>
<h5 id="关闭文件描述符"><a href="#关闭文件描述符" class="headerlink" title="关闭文件描述符"></a>关闭文件描述符</h5><p>将文件描述符重定向到特殊的文件描述符关闭<code>-</code>.使用<code>exec fd&gt;&amp;-</code>来关闭.</p>
<h2 id="数组"><a href="#数组" class="headerlink" title="数组"></a>数组</h2><pre class="line-numbers language-bash"><code class="language-bash">array_var<span class="token operator">=</span><span class="token punctuation">(</span>var0 var1 var2 <span class="token punctuation">..</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># 还可以</span>
array_var<span class="token punctuation">[</span>0<span class="token punctuation">]</span><span class="token operator">=</span>var0
array_var<span class="token punctuation">[</span>1<span class="token punctuation">]</span><span class="token operator">=</span>var1
<span class="token punctuation">..</span>.

<span class="token comment" spellcheck="true"># 打印</span>
<span class="token keyword">echo</span> <span class="token variable">${array_var[0]}</span> 
<span class="token keyword">echo</span> <span class="token variable">${array_var[$index]}</span>

<span class="token comment" spellcheck="true">#以列表形式打印出数组所有值</span>
<span class="token keyword">echo</span> <span class="token variable">${array_var[*]}</span> 
<span class="token comment" spellcheck="true">#或者</span>
<span class="token keyword">echo</span> <span class="token variable">${array_var[@]}</span> 
<span class="token comment" spellcheck="true"># 打印数组长度</span>
<span class="token keyword">echo</span> <span class="token variable">${#array_var[*]}</span> 

<span class="token comment" spellcheck="true"># 关联数组 key可以是字符串</span>
<span class="token comment" spellcheck="true"># 先声明</span>
<span class="token keyword">declare</span> -A ass_array
<span class="token comment" spellcheck="true">#赋值</span>
ass_array<span class="token operator">=</span><span class="token punctuation">(</span><span class="token punctuation">[</span>index0<span class="token punctuation">]</span><span class="token operator">=</span>var0 <span class="token punctuation">[</span>index1<span class="token punctuation">]</span><span class="token operator">=</span>var1<span class="token punctuation">)</span>
ass_array<span class="token punctuation">[</span>index1<span class="token punctuation">]</span><span class="token operator">=</span>var0
ass_array<span class="token punctuation">[</span>index1<span class="token punctuation">]</span><span class="token operator">=</span>var1

<span class="token comment" spellcheck="true"># example</span>
<span class="token keyword">declare</span> -A fruits_value
fruits_value<span class="token operator">=</span><span class="token punctuation">(</span><span class="token punctuation">[</span>apple<span class="token punctuation">]</span><span class="token operator">=</span><span class="token string">'100 dollars'</span> <span class="token punctuation">[</span>orange<span class="token punctuation">]</span><span class="token operator">=</span><span class="token string">'150 dollars'</span><span class="token punctuation">)</span>
<span class="token keyword">echo</span> <span class="token string">"Apple costs <span class="token variable">${fruits_value[apple]}</span>"</span>
Apple costs 100 dollars

<span class="token comment" spellcheck="true"># 获取索引(key)列表</span>
<span class="token keyword">echo</span> <span class="token variable">${!array_var[*]}</span> <span class="token comment" spellcheck="true">#也可以用@</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="创建别名"><a href="#创建别名" class="headerlink" title="创建别名"></a>创建别名</h4><pre class="line-numbers language-bash"><code class="language-bash"><span class="token comment" spellcheck="true"># 删除原始文件,并保留一个备份</span>
<span class="token function">alias</span> rm<span class="token operator">=</span><span class="token string">'cp <span class="token variable">$@</span> ~/backup &amp;&amp; rm <span class="token variable">$@</span>'</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<h4 id="禁止回显"><a href="#禁止回显" class="headerlink" title="禁止回显"></a>禁止回显</h4><pre class="line-numbers language-bash"><code class="language-bash"><span class="token shebang important">#!/bin/sh</span>
<span class="token comment" spellcheck="true">#Filename: password.sh</span>
<span class="token keyword">echo</span> -e <span class="token string">"Enter password: "</span>
<span class="token comment" spellcheck="true"># 在读取密码前禁止回显</span>
stty -echo
<span class="token function">read</span> password
<span class="token comment" spellcheck="true"># 重新允许回显</span>
stty <span class="token keyword">echo</span>
<span class="token keyword">echo</span>
<span class="token keyword">echo</span> Password read.
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="在脚本中延时"><a href="#在脚本中延时" class="headerlink" title="在脚本中延时"></a>在脚本中延时</h4><p>变量依次使用了由seq命令生成的一系列数字。我们用tput sc存储光标<br>位置。在每次循环中，通过tput rc恢复之前存储的光标位置，在终端中打印出新的count值，<br>然后使用tputs ed清除从当前光标位置到行尾之间的所有内容。行被清空之后，脚本就可以显<br>示出新的值。 sleep可以使脚本在每次循环迭代之间延迟1秒钟。 </p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token shebang important">#!/bin/bash</span>
<span class="token comment" spellcheck="true">#文件名: sleep.sh</span>
<span class="token keyword">echo</span> Count:
tput sc
<span class="token comment" spellcheck="true"># 循环40秒</span>
<span class="token comment" spellcheck="true"># 也可以</span>
<span class="token comment" spellcheck="true"># for count in {0..40};</span>
<span class="token keyword">for</span> count <span class="token keyword">in</span> <span class="token variable"><span class="token variable">`</span><span class="token function">seq</span> 0 40<span class="token variable">`</span></span> <span class="token comment" spellcheck="true"># 使用seq生成 0到40个数字</span>
<span class="token keyword">do</span>
tput rc
tput ed
<span class="token keyword">echo</span> -n <span class="token variable">$count</span>
<span class="token function">sleep</span> 1
<span class="token keyword">done</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="调试脚本"><a href="#调试脚本" class="headerlink" title="调试脚本"></a>调试脚本</h2><p>使用-x选项</p>
<blockquote>
<p>bash -x script.sh</p>
</blockquote>
<p>在Bash中，命令:告诉shell不要进行任何操作。 </p>
<p>DUBUG脚本</p>
<blockquote>
<p>[ “$_DEBUG” == “on” ] &amp;&amp; $@ || :</p>
</blockquote>
<h2 id="函数"><a href="#函数" class="headerlink" title="函数"></a>函数</h2><pre class="line-numbers language-bash"><code class="language-bash">fname<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">echo</span> <span class="token variable">$1</span>, <span class="token variable">$2</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">#访问参数1和参数2</span>
<span class="token keyword">echo</span> <span class="token string">"<span class="token variable">$@</span>"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">#以列表的方式一次性打印所有参数</span>
<span class="token keyword">echo</span> <span class="token string">"<span class="token variable">$*</span>"</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">#类似于$@，但是所有参数被视为单个实体</span>
<span class="token keyword">return</span> 0<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">#返回值</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<ul>
<li>$0是脚本名称。</li>
<li>$1是第一个参数。</li>
<li>$2是第二个参数。</li>
<li>$n是第n个参数。</li>
<li>“$@”被扩展成”$1” “$2” “$3”等。</li>
<li>“$*“被扩展成”$1c$2c$3”，其中c是IFS的第一个字符。*</li>
<li><em>“$@”要比”$\</em>“用得多。由于”$*“将所有的参数当作单个字符串，因此它很少被使用。 </li>
</ul>
<h4 id="报告命令是否成功"><a href="#报告命令是否成功" class="headerlink" title="报告命令是否成功"></a>报告命令是否成功</h4><pre class="line-numbers language-bash"><code class="language-bash"><span class="token shebang important">#!/bin/bash</span>
<span class="token comment" spellcheck="true">#文件名: success_test.sh</span>
<span class="token comment" spellcheck="true">#对命令行参数求值，比如success_test.sh ‘ls | grep txt’</span>
<span class="token function">eval</span> <span class="token variable">$@</span> <span class="token comment" spellcheck="true"># 解释执行</span>
<span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable">$?</span> -eq 0 <span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">then</span>
<span class="token keyword">echo</span> <span class="token string">"<span class="token variable">$CMD</span> executed successfully"</span>
<span class="token keyword">else</span>
<span class="token keyword">echo</span> <span class="token string">"<span class="token variable">$CMD</span> terminated unsuccessfully"</span>
<span class="token keyword">fi</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="迭代命令行参数"><a href="#迭代命令行参数" class="headerlink" title="迭代命令行参数"></a>迭代命令行参数</h4><pre class="line-numbers language-bash"><code class="language-bash"><span class="token comment" spellcheck="true"># shift命令可以将参数依次向左移动一个位置，让脚本能够使用$1来访问到每一个参数</span>
$ <span class="token function">cat</span> showArgs.sh
<span class="token keyword">for</span> i <span class="token keyword">in</span> `seq 1 $<span class="token comment" spellcheck="true">#` # $# 参数长度</span>
<span class="token keyword">do</span>
<span class="token keyword">echo</span> <span class="token variable">$i</span> is <span class="token variable">$1</span>
<span class="token function">shift</span>
<span class="token keyword">done</span>
$ sh showArgs.sh a b c
1 is a
2 is b
3 is c
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="保存变量"><a href="#保存变量" class="headerlink" title="保存变量"></a>保存变量</h2><pre class="line-numbers language-bash"><code class="language-bash">var<span class="token operator">=</span><span class="token variable"><span class="token variable">$(</span><span class="token function">ls</span><span class="token variable">)</span></span> <span class="token comment" spellcheck="true">#子shell法</span>
var<span class="token operator">=</span><span class="token variable"><span class="token variable">`</span><span class="token function">ls</span><span class="token variable">`</span></span> <span class="token comment" spellcheck="true"># 反引用法</span>
var<span class="token operator">=</span><span class="token string">"<span class="token variable"><span class="token variable">`</span><span class="token function">ls</span><span class="token variable">`</span></span>"</span> <span class="token comment" spellcheck="true"># 若要保存空格换行符,需使用双引号.</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<h2 id="read命令"><a href="#read命令" class="headerlink" title="read命令"></a>read命令</h2><pre class="line-numbers language-bash"><code class="language-bash"><span class="token comment" spellcheck="true">#下面的语句从输入中读取n个字符并存入变量variable_name：</span>
<span class="token function">read</span> -n 2 var
<span class="token keyword">echo</span> <span class="token variable">$var</span>
<span class="token comment" spellcheck="true"># 用无回显的方式读取密码：</span>
<span class="token function">read</span> -s var
<span class="token comment" spellcheck="true"># 使用read显示提示信息：</span>
<span class="token function">read</span> -p <span class="token string">"Enter input:"</span> var
<span class="token comment" spellcheck="true"># 在给定时限内读取输入：</span>
<span class="token function">read</span> -t <span class="token function">timeout</span> var
<span class="token comment" spellcheck="true"># 在2秒内将键入的字符串读入变量var</span>
<span class="token function">read</span> -t 2 var

<span class="token comment" spellcheck="true"># 用特定的定界符作为输入行的结束：</span>
<span class="token function">read</span> -d delim_char var
<span class="token comment" spellcheck="true"># hello: #var被设置为hello</span>
$ <span class="token function">read</span> -d <span class="token string">":"</span> var
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="重复执行直到成功"><a href="#重复执行直到成功" class="headerlink" title="重复执行直到成功"></a>重复执行直到成功</h4><p><code>:</code>该命令的退出状态总是为0,该命令什么也不做</p>
<pre class="line-numbers language-bash"><code class="language-bash">repeat<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">while</span> <span class="token keyword">:</span><span class="token punctuation">;</span> <span class="token keyword">do</span> <span class="token variable">$@</span> <span class="token operator">&amp;&amp;</span> <span class="token keyword">return</span><span class="token punctuation">;</span> <span class="token keyword">done</span> <span class="token punctuation">}</span>
<span class="token comment" spellcheck="true"># 加入延时</span>
repeat<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">while</span> <span class="token keyword">:</span><span class="token punctuation">;</span> <span class="token keyword">do</span> <span class="token variable">$@</span> <span class="token operator">&amp;&amp;</span> <span class="token keyword">return</span><span class="token punctuation">;</span> <span class="token function">sleep</span> 30<span class="token punctuation">;</span> <span class="token keyword">done</span> <span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<h2 id="字段分隔符IFS"><a href="#字段分隔符IFS" class="headerlink" title="字段分隔符IFS"></a>字段分隔符IFS</h2><pre class="line-numbers language-bash"><code class="language-bash">data<span class="token operator">=</span><span class="token string">"name, gender,rollno,location"</span>

oldIFS<span class="token operator">=</span><span class="token variable">$IFS</span>
IFS<span class="token operator">=</span>, <span class="token comment" spellcheck="true">#IFS现在被设置为,</span>
<span class="token keyword">for</span> item <span class="token keyword">in</span> <span class="token variable">$data</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true"># 分隔符改为,号</span>
<span class="token keyword">do</span>
<span class="token keyword">echo</span> Item: <span class="token variable">$item</span>
<span class="token keyword">done</span>
IFS<span class="token operator">=</span><span class="token variable">$oldIFS</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h3 id="常用循环"><a href="#常用循环" class="headerlink" title="常用循环"></a>常用循环</h3><pre class="line-numbers language-bash"><code class="language-bash"><span class="token comment" spellcheck="true"># 面向列表的for循环</span>
<span class="token comment" spellcheck="true"># list可以是一个字符串，也可以是一个值序列。</span>
<span class="token keyword">for</span> var <span class="token keyword">in</span> list<span class="token punctuation">;</span>
<span class="token keyword">do</span>
commands<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">#使用变量$var</span>
<span class="token keyword">done</span>

<span class="token comment" spellcheck="true"># 生成值序列</span>
<span class="token keyword">echo</span> <span class="token punctuation">{</span>1<span class="token punctuation">..</span>50<span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">#生成一个从1~50的数字序列</span>
<span class="token keyword">echo</span> <span class="token punctuation">{</span>a<span class="token punctuation">..</span>z<span class="token punctuation">}</span> <span class="token punctuation">{</span>A<span class="token punctuation">..</span>Z<span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">#生成大小写字母序列</span>

<span class="token comment" spellcheck="true"># 迭代指定范围的数字</span>
<span class="token keyword">for</span><span class="token variable"><span class="token punctuation">((</span>i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span><span class="token number">10</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">))</span></span>
<span class="token punctuation">{</span>
 commands<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">#使用变量$i</span>
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true">#循环到条件满足为止</span>
<span class="token comment" spellcheck="true">#当条件为真时， while循环继续执行；当条件不为真时， until循环继续执行。用true作为循环条件能够产生无限循环。</span>
<span class="token keyword">while</span> condition
<span class="token keyword">do</span>
commands<span class="token punctuation">;</span>
<span class="token keyword">done</span>

<span class="token comment" spellcheck="true">#在Bash中还可以使用一个特殊的循环until。它会一直循环，直到给定的条件为真。例如：</span>
x<span class="token operator">=</span>0<span class="token punctuation">;</span>
<span class="token keyword">until</span> <span class="token punctuation">[</span> <span class="token variable">$x</span> -eq 9 <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">#条件是[$x -eq 9 ]</span>
<span class="token keyword">do</span>
<span class="token keyword">let</span> x++<span class="token punctuation">;</span> <span class="token keyword">echo</span> <span class="token variable">$x</span><span class="token punctuation">;</span>
<span class="token keyword">done</span>

<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="比较测试"><a href="#比较测试" class="headerlink" title="比较测试"></a>比较测试</h2><pre class="line-numbers language-bash"><code class="language-bash"><span class="token comment" spellcheck="true"># if条件</span>
<span class="token keyword">if</span> condition<span class="token punctuation">;</span> <span class="token keyword">then</span>
commands<span class="token punctuation">;</span>
<span class="token keyword">else</span> <span class="token keyword">if</span> condition<span class="token punctuation">;</span> <span class="token keyword">then</span>
commands<span class="token punctuation">;</span>
<span class="token keyword">else</span>
commands<span class="token punctuation">;</span>
<span class="token keyword">fi</span>

<span class="token punctuation">[</span> condition <span class="token punctuation">]</span> <span class="token operator">&amp;&amp;</span> action<span class="token punctuation">;</span> <span class="token comment" spellcheck="true"># 如果condition为真，则执行action</span>
<span class="token punctuation">[</span> condition <span class="token punctuation">]</span> <span class="token operator">||</span> action<span class="token punctuation">;</span> <span class="token comment" spellcheck="true"># 如果condition为假，则执行action</span>

<span class="token comment" spellcheck="true"># 算数比较</span>
<span class="token comment" spellcheck="true">#-gt：大于。</span>
<span class="token comment" spellcheck="true">#-lt：小于。</span>
<span class="token comment" spellcheck="true">#-ge：大于或等于。</span>
<span class="token comment" spellcheck="true">#-le：小于或等于。</span>
<span class="token comment" spellcheck="true">#-a是逻辑与操作符， -o是逻辑或操作符。</span>

<span class="token punctuation">[</span> <span class="token variable">$var</span> -eq 0 <span class="token punctuation">]</span> <span class="token comment" spellcheck="true">#当$var等于0时，返回真</span>
<span class="token punctuation">[</span> <span class="token variable">$var</span> -ne 0 <span class="token punctuation">]</span> <span class="token comment" spellcheck="true">#当$var不为0时，返回真</span>
<span class="token punctuation">[</span> <span class="token variable">$var1</span> -ne 0 -a <span class="token variable">$var2</span> -gt 2 <span class="token punctuation">]</span> <span class="token comment" spellcheck="true">#使用逻辑与-a</span>
<span class="token punctuation">[</span> <span class="token variable">$var1</span> -ne 0 -o <span class="token variable">$var2</span> -gt 2 <span class="token punctuation">]</span> <span class="token comment" spellcheck="true">#逻辑或-o</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>文件比较条件</p>
<p>● [ -f $file_var ]：如果给定的变量包含正常的文件路径或文件名，则返回真。<br>● [ -x $var ]：如果给定的变量包含的文件可执行，则返回真。<br>● [ -d $var ]：如果给定的变量包含的是目录，则返回真。<br>● [ -e $var ]：如果给定的变量包含的文件存在，则返回真。<br>● [ -c $var ]：如果给定的变量包含的是一个字符设备文件的路径，则返回真。<br>● [ -b $var ]：如果给定的变量包含的是一个块设备文件的路径，则返回真。<br>● [ -w $var ]：如果给定的变量包含的文件可写，则返回真。<br>● [ -r $var ]：如果给定的变量包含的文件可读，则返回真。<br>● [ -L $var ]：如果给定的变量包含的是一个符号链接，则返回真。 </p>
<h4 id="字符串比较"><a href="#字符串比较" class="headerlink" title="字符串比较"></a>字符串比较</h4><p>进行字符串比较时，最好用双中括号，因为有时候采用单个中括号会产生错误。 </p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token comment" spellcheck="true"># 当str1等于str2时，返回真。也就是说， str1和str2包含的文本是一模一样的。</span>
<span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token variable">$str1</span> <span class="token operator">=</span> <span class="token variable">$str2</span> <span class="token punctuation">]</span><span class="token punctuation">]</span>：<span class="token comment" spellcheck="true"># 前后一个空格,否则是赋值语句</span>
<span class="token comment" spellcheck="true"># 这是检查字符串是否相同的另一种写法。</span>
<span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token variable">$str1</span> <span class="token operator">==</span> <span class="token variable">$str2</span> <span class="token punctuation">]</span><span class="token punctuation">]</span>：
<span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token variable">$str1</span> <span class="token operator">></span> <span class="token variable">$str2</span> <span class="token punctuation">]</span><span class="token punctuation">]</span>：<span class="token comment" spellcheck="true">#如果str1的字母序比str2大，则返回真。</span>
<span class="token punctuation">[</span><span class="token punctuation">[</span> <span class="token variable">$str1</span> <span class="token operator">&lt;</span> <span class="token variable">$str2</span> <span class="token punctuation">]</span><span class="token punctuation">]</span>：<span class="token comment" spellcheck="true">#如果str1的字母序比str2小，则返回真。</span>
<span class="token punctuation">[</span><span class="token punctuation">[</span> -z <span class="token variable">$str1</span> <span class="token punctuation">]</span><span class="token punctuation">]</span>：<span class="token comment" spellcheck="true">#如果str1为空串，则返回真。</span>
<span class="token punctuation">[</span><span class="token punctuation">[</span> -n <span class="token variable">$str1</span> <span class="token punctuation">]</span><span class="token punctuation">]</span>：<span class="token comment" spellcheck="true">#如果str1不为空串，则返回真。</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="test命令测试"><a href="#test命令测试" class="headerlink" title="test命令测试"></a>test命令测试</h4><pre class="line-numbers language-bash"><code class="language-bash"><span class="token keyword">if</span> <span class="token punctuation">[</span> <span class="token variable">$var</span> -eq 0 <span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">then</span> <span class="token keyword">echo</span> <span class="token string">"True"</span><span class="token punctuation">;</span> <span class="token keyword">fi</span>
<span class="token comment" spellcheck="true"># 等于</span>
<span class="token keyword">if</span> <span class="token function">test</span> <span class="token variable">$var</span> -eq 0 <span class="token punctuation">;</span> <span class="token keyword">then</span> <span class="token keyword">echo</span> <span class="token string">"True"</span><span class="token punctuation">;</span> <span class="token keyword">fi</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<p>[ 是bash的内部函数,而test是外部程序,因此[效率更高</p>
<h3 id="文本处理"><a href="#文本处理" class="headerlink" title="文本处理"></a>文本处理</h3><p>行地址对于任何地址都是有效的<code>[address]command</code>.命令也可以是多行的.命令也可以用<code>;</code>隔开.</p>
<pre class="line-numbers language-bash"><code class="language-bash">address<span class="token punctuation">{</span>
    command1
    command2
    command3
<span class="token punctuation">}</span>
<span class="token comment" spellcheck="true"># 地址可以是行</span>

<span class="token comment" spellcheck="true"># 替换第2和3三行</span>
<span class="token function">sed</span> <span class="token string">'2,3s/dog/cat/'</span> data.txt
<span class="token comment" spellcheck="true"># $表示最后一行,若要表示第一行可以用0</span>
<span class="token function">sed</span> <span class="token string">'2,<span class="token variable">$s</span>/dog/cat/'</span> data.txt
<span class="token comment" spellcheck="true"># 文本模式 /pattern/,/pattern/</span>
<span class="token comment" spellcheck="true">#也可以用正则或相关内容来进行匹配</span>
<span class="token function">sed</span> <span class="token string">'/root/s/bash/zsh/'</span> /etc/passwd

<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="sed"><a href="#sed" class="headerlink" title="sed"></a>sed</h4><h4 id="常用选项"><a href="#常用选项" class="headerlink" title="常用选项"></a>常用选项</h4><ul>
<li>-n 禁止输出,如要输出,可以用<code>p</code>命令</li>
</ul>
<h5 id="替换命令-s-pattern-replacement-flags-定界符可以是任意字符-例如"><a href="#替换命令-s-pattern-replacement-flags-定界符可以是任意字符-例如" class="headerlink" title="替换命令 s/pattern/replacement/flags.定界符可以是任意字符,例如;"></a>替换命令 s/pattern/replacement/flags.定界符可以是任意字符,例如<code>;</code></h5><p>replacement中的特殊字符</p>
<ul>
<li>&amp; 用正则表达式匹配的内容进行替换</li>
<li>\ [num] 用\加一个数字,可以配置pattern中用\( \)指定的第几个匹配</li>
</ul>
<p>flags</p>
<ul>
<li>数字，表明新文本将替换第几处模式匹配的地方；</li>
<li>g，表明新文本将会替换所有匹配的文本；</li>
<li>p，表明原先行的内容要打印出来；</li>
<li>w file，将替换的结果写到文件中 </li>
</ul>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token comment" spellcheck="true"># examples</span>
$ <span class="token function">sed</span> <span class="token string">'s/test/trial/'</span> data4.txt
This is a trial of the <span class="token function">test</span> script.
This is the second trial of the <span class="token function">test</span> script.

<span class="token comment" spellcheck="true"># 使用数字方式寻址</span>
$ <span class="token function">sed</span> <span class="token string">'2s/dog/cat/'</span> data1.txt
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy <span class="token function">cat</span>
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog

<span class="token comment" spellcheck="true"># 文本过滤器模式 /pattern/command</span>
<span class="token comment" spellcheck="true">#</span>
<span class="token comment" spellcheck="true"># Samantha:x:502:502::/home/Samantha:/bin/bash</span>
<span class="token comment" spellcheck="true"># 用冒号当作分隔定界符</span>
$ <span class="token function">sed</span> <span class="token string">'/Samantha/s:bash:csh:'</span> /etc/passwd
Christine:x:501:501:Christine B:/home/Christine:/bin/bash
Samantha:x:502:502::/home/Samantha:/bin/csh
Timothy:x:503:503::/home/Timothy:/bin/bash
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h5 id="删除行-d"><a href="#删除行-d" class="headerlink" title="删除行 d"></a>删除行 d</h5><p>会删除当前模式空间中的行内容.</p>
<pre class="line-numbers language-bash"><code class="language-bash">$ <span class="token function">sed</span> <span class="token string">'3d'</span> data6.txt
This is line number 1.
This is line number 2.
This is line number 4.

<span class="token comment" spellcheck="true"># 可用两个文本模式来删除某个区间内的行</span>
<span class="token comment" spellcheck="true"># 但注意: 第一个模式会打开删除模式,第二个模式会关闭删除模式</span>
<span class="token comment" spellcheck="true"># 如果第二个模式没有匹配,会删除剩下的所有内容!!</span>
<span class="token function">sed</span> <span class="token string">'/1/,/3/d'</span> /etc/passwd
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h5 id="插入和附加文本-address-command-new-line"><a href="#插入和附加文本-address-command-new-line" class="headerlink" title="插入和附加文本  [address]command\ new line"></a>插入和附加文本  [address]command\ new line</h5><ul>
<li>插入（insert）命令（i）会在指定行前增加一个新行； </li>
<li>附加（append）命令（a）会在指定行后增加一个新行 </li>
</ul>
<pre class="line-numbers language-bash"><code class="language-bash">$ <span class="token keyword">echo</span> <span class="token string">"Test Line 2"</span> <span class="token operator">|</span> <span class="token function">sed</span> <span class="token string">'i\Test Line 1'</span>
Test Line 1
Test Line 2

$ <span class="token keyword">echo</span> <span class="token string">"Test Line 2"</span> <span class="token operator">|</span> <span class="token function">sed</span> <span class="token string">'a\Test Line 1'</span>
Test Line 2
Test Line 1
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h5 id="修改行-address-c-new-line-匹配的行被删除"><a href="#修改行-address-c-new-line-匹配的行被删除" class="headerlink" title="修改行 [address] c \ new line 匹配的行被删除"></a>修改行 [address] c \ new line 匹配的行被删除</h5><pre class="line-numbers language-bash"><code class="language-bash">$ <span class="token function">sed</span> <span class="token string">'3c\
> This is a changed line of text.'</span> data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.

$ <span class="token function">sed</span> <span class="token string">'/number 3/c\
> This is a changed line of text.'</span> data6.txt
This is line number 1.
This is line number 2.
This is a changed line of text.
This is line number 4.
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h5 id="转换命令-transform"><a href="#转换命令-transform" class="headerlink" title="转换命令[transform]"></a>转换命令[transform]</h5><p> [address]y/inchars/outchars/  </p>
<p>inchars和outchars长度要一致.in中的字符会被相应的out字符替换.</p>
<pre class="line-numbers language-bash"><code class="language-bash">$ <span class="token function">sed</span> <span class="token string">'y/123/789/'</span> data8.txt
This is line number 7.
This is line number 8.
This is line number 9.
This is line number 4.
This is line number 7 again.
This is yet another line.
This is the last line <span class="token keyword">in</span> the file.
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h5 id="其他命令"><a href="#其他命令" class="headerlink" title="其他命令"></a>其他命令</h5><p>p命令用来打印文本行</p>
<p>=用来打印行号</p>
<p>l(小写的L)用来列出行. 这个可以打印出数据流中不可打印的字符.例如制表符会被用<code>\t</code>来代替.</p>
<h5 id="读取命令r"><a href="#读取命令r" class="headerlink" title="读取命令r"></a>读取命令r</h5><p>[addr]r filename</p>
<p>允许你从一个文件中读取内容并插入数据流中.在地址后面追加</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">sed</span> <span class="token string">'3r data.txt'</span> /etc/passwd
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<h4 id="sed进阶"><a href="#sed进阶" class="headerlink" title="sed进阶"></a>sed进阶</h4><h5 id="多行处理"><a href="#多行处理" class="headerlink" title="多行处理"></a>多行处理</h5><ul>
<li>N 将数据流中的下一行加进来创建一个多行组</li>
<li>D 删除多行组的一行</li>
<li>P 打印多行组的一行</li>
</ul>
<h5 id="next-n-用来移动数据流到下一行"><a href="#next-n-用来移动数据流到下一行" class="headerlink" title="next n 用来移动数据流到下一行"></a>next n 用来移动数据流到下一行</h5><pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">cat</span> data
a

b

c

<span class="token comment" spellcheck="true"># 若想只删除a后面的空行,可用n命令</span>
<span class="token function">sed</span> <span class="token string">'/a/{ n ; d}'</span> data
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>N命令会将下一行也放入到当前的模式空间中,意思就是说,当前行和下一行内容同时存在,他们中间用<code>\n</code>隔开</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">cat</span> data
a
b
c

<span class="token function">sed</span> <span class="token string">'/a/{ N ; s/\n/ /}'</span> data
a b
c
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>D命令会删除模式空间的第一行.就是会删除到换行符(包含换行符)</p>
<p>D命令会显示模式空间的第一行.就是会删除到换行符(包含换行符)</p>
<h5 id="保持空间"><a href="#保持空间" class="headerlink" title="保持空间"></a>保持空间</h5><p>模式空间是一块缓冲区,它保存当前待检测的文本内容(可能不是一行,例如用了<code>N</code>命令)</p>
<p>保存空间是另一块缓冲区,可以用它来保存一些临时行</p>
<p><img src="image-20200319210112330.png" alt="image-20200319210112330"></p>

            </div>
            <hr>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="http://localhost:4000" rel="external nofollow noreferrer">yyancy</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="http://localhost:4000/2020/04/01/shell-xiang-guan-cao-zuo/">http://localhost:4000/2020/04/01/shell-xiang-guan-cao-zuo/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="http://localhost:4000" target="_blank">yyancy</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/Linux/">
                                    <span class="chip bg-color">Linux</span>
                                </a>
                            
                                <a href="/tags/shell/">
                                    <span class="chip bg-color">shell</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">

<div id="article-share">
    
    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
        </div>
    </div>

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="far fa-dot-circle"></i>&nbsp;本篇
            </div>
            <div class="card">
                <a href="/2020/04/01/shell-xiang-guan-cao-zuo/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/6.jpg" class="responsive-img" alt="shell相关操作">
                        
                        <span class="card-title">shell相关操作</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            echo# 默认会换行
echo "welcome to bash" 
echo 'welcome to bash'

# 双引号可以解释变量,特殊字符.单引号只当做字符串
echo "$SHELL" # /bin/bash
echo '$
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2020-04-01
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-user fa-fw"></i>
                            yyancy
                            
                        </span>
                    </div>
                </div>

                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Linux/">
                        <span class="chip bg-color">Linux</span>
                    </a>
                    
                    <a href="/tags/shell/">
                        <span class="chip bg-color">shell</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2020/04/01/vim-xiang-guan-cao-zuo/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/4.jpg" class="responsive-img" alt="vim相关操作">
                        
                        <span class="card-title">vim相关操作</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            esc和C-[相同,都是取消模式
ctrl-R撤销之前的撤销操作,U撤销一行的修改
ctrl-G显示当前文件的信息
ctrl-l/下翻一页;ctrl-B上翻一页;ctrl-D下翻半页;ctrl-U上翻半页;ctrl-E向下一行;ctrl-Y
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2020-04-01
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-user fa-fw"></i>
                            yyancy
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/vim/">
                        <span class="chip bg-color">vim</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

    
<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->


<!-- 代码块折行 -->

<style type="text/css">
code[class*="language-"], pre[class*="language-"] { white-space: pre !important; }
</style>

    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>



    <footer class="page-footer bg-color">

    <div class="container row center-align" style="margin-bottom: 15px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            <span id="year">2019</span>
            <a href="http://localhost:4000" target="_blank">yyancy</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
            &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span class="white-color">5.2k</span>&nbsp;字
            
            
            
            
            
            <script src="/js/busuanzi.pure.mini.js"></script>
<!--            <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>-->
            
            <span id="busuanzi_container_site_pv" style="display:block">
                |&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;<span id="busuanzi_value_site_pv" class="white-color"></span>&nbsp;次
            </span>
            
            
            <span id="busuanzi_container_site_uv" style="display:block">
                |&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;<span id="busuanzi_value_site_uv" class="white-color"></span>&nbsp;人
            </span>
            
            <br>
<!-- 不蒜子计数初始值纠正 -->
<script>
    $(document).ready(function () {
        $("#busuanzi_value_site_pv,#busuanzi_value_site_uv").show();
        var int = setInterval(fixCount, 50);
        var pvcountOffset = 0;
        var uvcountOffset = 0;
        function fixCount() {
            if (document.getElementById("busuanzi_container_site_pv").style.display != "none") {
                $("#busuanzi_value_site_pv").html(parseInt($("#busuanzi_value_site_pv").html()) + pvcountOffset);
                clearInterval(int);
            }
            if ($("#busuanzi_container_site_pv").css("display") != "none") {
                $("#busuanzi_value_site_uv").html(parseInt($("#busuanzi_value_site_uv").html()) + uvcountOffset); // 加上初始数据
                clearInterval(int);
            }
        }
    });
</script>



            
            <span id="sitetime">载入运行时间...</span>
            <script>
                function siteTime() {
                    var seconds = 1000;
                    var minutes = seconds * 60;
                    var hours = minutes * 60;
                    var days = hours * 24;
                    var years = days * 365;
                    var today = new Date();
                    var startYear = "2019";
                    var startMonth = "6";
                    var startDate = "28";
                    var startHour = "0";
                    var startMinute = "0";
                    var startSecond = "0";
                    var todayYear = today.getFullYear();
                    var todayMonth = today.getMonth() + 1;
                    var todayDate = today.getDate();
                    var todayHour = today.getHours();
                    var todayMinute = today.getMinutes();
                    var todaySecond = today.getSeconds();
                    var t1 = Date.UTC(startYear, startMonth, startDate, startHour, startMinute, startSecond);
                    var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond);
                    var diff = t2 - t1;
                    var diffYears = Math.floor(diff / years);
                    var diffDays = Math.floor((diff / days) - diffYears * 365);
                    var diffHours = Math.floor((diff - (diffYears * 365 + diffDays) * days) / hours);
                    var diffMinutes = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours) /
                        minutes);
                    var diffSeconds = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours -
                        diffMinutes * minutes) / seconds);
                    if (startYear == todayYear) {
                        document.getElementById("year").innerHTML = todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffDays + " 天 " + diffHours +
                            " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    } else {
                        document.getElementById("year").innerHTML = startYear + " - " + todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffYears + " 年 " + diffDays +
                            " 天 " + diffHours + " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    }
                }
                setInterval(siteTime, 1000);
            </script>
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/yyancy" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:yyancy517@gmail.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=1181062873" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 1181062873" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







    <a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fas fa-rss"></i>
    </a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字" class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script src="/js/search.js"></script>
<script type="text/javascript">
$(function () {
    searchFunc("/" + "search.xml", 'searchInput', 'searchResult');
});
</script>
    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    <!-- Global site tag (gtag.js) - Google Analytics -->


    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    

    

    
    
    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"log":false,"model":{"jsonPath":"/live2dw/assets/shizuku.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":true},"react":{"opacity":0.7}});</script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body>

</html>
